package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import dto.LogonDTO;
import dto.UserDTO;

public class EditUserPage extends JDialog {
	private static final long serialVersionUID = 1L;

	private JLabel labelMessage;
	private JTextField textFieldName;
	private JTextField textFieldUsername;
	private JComboBox comboBoxLevel;
	private JTextField textFieldEditorUsername;
	private JTextField textFieldEditorLevel;

	private JButton btnUpdateButton = new JButton("Update");
	private JButton btnBackButton = new JButton("Back");

	private UserDTO userDTO;

	public UserDTO getUserDTO() {
		userDTO.setUserName(textFieldName.getText().trim());
		userDTO.setAccessLevel(comboIndexToLevel(comboBoxLevel
				.getSelectedIndex()));
		return userDTO;
	}

	/**
	 * Create the dialog.
	 */
	public EditUserPage(LogonDTO logonDTO, UserDTO userDTO) {

		this.userDTO = userDTO;

		setBounds(100, 100, 450, 278);
		getContentPane().setLayout(
				new MigLayout("", "[grow][grow][grow][][]",
						"[][][][][][][][][][]"));
		{
			JLabel lblJaLibrary = new JLabel("J&A Library");
			lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
			lblJaLibrary.setForeground(Color.BLUE);
			getContentPane().add(lblJaLibrary, "cell 0 0 5 1,alignx center");
		}
		{
			JLabel lblEditUser = new JLabel("Edit User");
			getContentPane().add(lblEditUser, "cell 0 1 5 1,alignx center");
		}
		{
			JLabel label = new JLabel("     ");
			getContentPane().add(label, "cell 0 2");
		}
		{
			JLabel lblName = new JLabel("Name:");
			getContentPane().add(lblName, "cell 0 3,alignx trailing");
		}
		{
			textFieldName = new JTextField();
			textFieldName.setText(userDTO.getUserName());
			getContentPane().add(textFieldName, "cell 1 3 3 1,growx");
			textFieldName.setColumns(10);
		}
		{
			JLabel lblUsername = new JLabel("Username:");
			getContentPane().add(lblUsername, "cell 0 4,alignx trailing");
		}
		{
			textFieldUsername = new JTextField();
			textFieldUsername.setEditable(false);
			textFieldUsername.setText(userDTO.getUserId());
			getContentPane().add(textFieldUsername, "cell 1 4,growx");
			textFieldUsername.setColumns(10);
		}
		{
			JLabel lblAccessLevel = new JLabel("Access Level:");
			getContentPane().add(lblAccessLevel, "cell 0 5,alignx trailing");
		}
		{
			comboBoxLevel = new JComboBox();
			comboBoxLevel.setModel(new DefaultComboBoxModel(new String[] {
					"Member", "Librarian", "Administrator" }));
			comboBoxLevel.setSelectedIndex(comboLevelToIndex(userDTO
					.getAccessLevel()));
			getContentPane().add(comboBoxLevel, "cell 1 5,growx");
		}
		{
			JLabel label = new JLabel("     ");
			getContentPane().add(label, "cell 0 6");
		}

		// UPDATE button
		getContentPane().add(btnUpdateButton, "cell 0 7,growx");

		// BACK button
		btnBackButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		getContentPane().add(btnBackButton, "cell 4 7");
		{
			labelMessage = new JLabel("     ");
			labelMessage.setForeground(Color.BLUE);
			labelMessage.setFont(new Font("Arial", Font.BOLD, 12));
			getContentPane().add(labelMessage, "cell 0 8 5 1");
		}
		{
			textFieldEditorUsername = new JTextField();
			textFieldEditorUsername.setEditable(false);
			textFieldEditorUsername.setText(logonDTO.getUserName());
			getContentPane().add(textFieldEditorUsername, "cell 0 9 2 1,growx");
			textFieldEditorUsername.setColumns(10);
		}
		{
			textFieldEditorLevel = new JTextField();
			textFieldEditorLevel.setEditable(false);
			textFieldEditorLevel.setText(logonDTO.getUserTitle());
			getContentPane().add(textFieldEditorLevel, "cell 2 9 3 1,growx");
			textFieldEditorLevel.setColumns(10);
		}
	}

	private int comboLevelToIndex(int level) {
		switch (level) {
		case 1:
			return 2;
		case 2:
			return 1;
		case 3:
			return 0;
			// case 3:
			// return 0;
		}
		// return -1;
		return 0;
	}

	private int comboIndexToLevel(int index) {
		switch (index) {
		case 0:
			return 3;
		case 1:
			return 2;
		case 2:
			return 1;
			// case 3:
			// return 0;
		}
		// return -1;
		return 0;
	}

	public void addUpdateUserButtonListener(ActionListener al) {
		btnUpdateButton.addActionListener(al);
	}

	public void displayMessage(String message) {
		labelMessage.setText(message);
	}

}
